package com.musicapp;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class ShanpingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shanpingye);
        isFirstStart();
    }

    public void isFirstStart() {
        SharedPreferences preferences = getSharedPreferences("NB_FIRST_START", 0);
        boolean isFirst = preferences.getBoolean("FIRST_START", true);
        if (isFirst) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            //获取界面
            View view = LayoutInflater.from(this).inflate(R.layout.tiaokuan, null);
            //将界面填充到AlertDiaLog容器并去除边框
            builder.setView(view);
            //取消点击外部消失弹窗
            builder.setCancelable(false);
            //创建AlertDiaLog
            builder.create();
            //AlertDiaLog显示
            AlertDialog dialog = builder.show();
            // 移除dialog的decorview背景色
            dialog.getWindow().getDecorView().setBackground(null);
            //初始化控件
            TextView but_ok = view.findViewById(R.id.agree);
            TextView but_return = view.findViewById(R.id.disagree);

            TextView YongHu = view.findViewById(R.id.yonghu);
            YongHu.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
//            YongHu.getPaint().setAntiAlias(true);//抗锯齿
            TextView YinSi = view.findViewById(R.id.yinsi);
            YinSi.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);//下划线
//            YinSi.getPaint().setAntiAlias(true);//抗锯齿
            //同意按钮
            but_ok.setOnClickListener(view1 -> {
                preferences.edit().putBoolean("FIRST_START", false).apply();
                dialog.dismiss();
                startActivity(new Intent(ShanpingActivity.this, MainActivity.class));
                finish();
            });

            //取消按钮
            but_return.setOnClickListener(view1 -> {
                dialog.dismiss();
                finish();
            });

            //用户协议
            YongHu.setOnClickListener(view1 -> {
                Toast.makeText(ShanpingActivity.this,"查看用户协议",Toast.LENGTH_SHORT).show();
            });

            //隐私政策
            YinSi.setOnClickListener(view1 -> {
                Toast.makeText(ShanpingActivity.this,"查看隐私协议",Toast.LENGTH_SHORT).show();
            });
        } else {
            preferences.edit().putBoolean("FIRST_START", false).apply();
            Handler handler = new Handler();
            handler.postDelayed(() -> {
                startActivity(new Intent(ShanpingActivity.this, MainActivity.class));
                finish();
            }, 2000);
        }
    }

//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if(keyCode == KeyEvent.KEYCODE_BACK){
//            return true;
//        }
//        return super.onKeyDown(keyCode, event);
//    }
}